package com.lych.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lych.dubbo.mappers.UserInfoMapper;
import com.lych.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 保存用户信息
     * @param userInfo
     */
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    /**
     * 首次登录---补充头像
     * @param userInfo
     */
    @Override
    public void updatehead(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 用户资料 - 读取
     * @param userID
     * @return
     */
    @Override
    public UserInfo findById(Long userID) {
        UserInfo userInfo = userInfoMapper.selectById(userID);
        return userInfo;
    }

    /**
     * 更新用户资料
     * @param userInfo
     */
    @Override
    public void updateUserInfo(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 批量查询用户的详细信息
     * @param ids
     * @param userInfo
     * @return
     */
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> ids, UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.in(UserInfo::getId,ids);
        if (userInfo != null) {
            if (userInfo.getAge() != null) {
                lqw.lt(UserInfo::getAge,userInfo.getAge());
            }
            if (userInfo.getGender() != null) {
                lqw.eq(UserInfo::getGender,userInfo.getGender());
            }
            if (userInfo.getNickname() != null) {
                lqw.like(UserInfo::getNickname,userInfo.getNickname());
            }
        }
        List<UserInfo> userInfos = userInfoMapper.selectList(lqw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(userInfos, "id");
        return map;
    }

    /**
     * 用户数据翻页
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public IPage<UserInfo> findAll(Integer page, Integer pagesize) {
        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(new Page<UserInfo>(page, pagesize), null);
        return userInfoPage;
    }
}
